package org.piggy.test.http.controller;

import org.piggy.client.autoconfigure.ApiProperties;
import org.piggy.client.core.ApiProtocol;
import org.piggy.client.core.ApiService;
import org.piggy.test.http.component.FileSaveHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ApiService(serviceId = "gateway-test-http-001", protocol = ApiProtocol.HTTP, patternPath = "/http-demo/**")
public class HttpDemoFirstController {

    private FileSaveHandler fileSaveHandler;

    @Autowired
    public void setFileSaveHandler(FileSaveHandler fileSaveHandler) {
        this.fileSaveHandler = fileSaveHandler;
    }

    public void setApiProperties(ApiProperties apiProperties) {}

    @GetMapping("/http-demo/ping")
    public String ping() {
        return "ping";
    }

    @PostMapping("/http-demo/echo")
    public String echo(String echo) throws InterruptedException {
        Thread.sleep(2000);
        return echo;
    }

    @PostMapping("/http-demo/upload")
    public Map<String,Object> uploadFiles(@RequestParam("files") MultipartFile[] files) {
        List<Map<String,Object>> fileList = new ArrayList<>();
        for (MultipartFile file : files) {
            try {
                String filePath = fileSaveHandler.uploadFile(file);
                if(filePath!=null){
                    Map<String,Object> map = new HashMap<>();
                    map.put("fileName",file.getName());
                    map.put("filePath",filePath);
                    fileList.add(map);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new HashMap<>() {{
            this.put("data", fileList);
        }};
    }

    @GetMapping("/http-demo/download")
    public void downloadFile(HttpServletResponse response) throws IOException {
        File file = new File("C:\\Users\\smart\\Pictures\\picture\\3gnznollyua.jpg");
        if (!file.exists()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found");
            return;
        }

        // 设置响应头
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
        response.setContentLengthLong(file.length());

        // 写入文件流
        try (InputStream inputStream = new FileInputStream(file);
             OutputStream outputStream = response.getOutputStream()) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }
    }
}
